﻿using System;
using System.Diagnostics;
using System.IO;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKCodeBase.WinForm;

namespace ShowGridNumber
{
	public class ShowGridApplication : IExternalApplication
	{
		public bool DrawGridNumber { get; set; }

		public Result OnStartup(UIControlledApplication application)
		{
			ShowGridApplication.m_ThisApp = this;
			ShowGridApplication.m_gridNumberShowForm = new GridNumberForm(ShowGridApplication.m_ThisApp);
			application.ViewActivated += this.OnRefreshGridsAndViewInfo;
			application.Idling += this.OnIdlingEvent;
			this.ReadShowState();
			if (this.m_hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				this.m_hRevitHandle = currentProcess.MainWindowHandle;
				this.m_hWndRevit = new WindowHandle(this.m_hRevitHandle);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result OnShutdown(UIControlledApplication application)
		{
			application.ViewActivated -= this.OnRefreshGridsAndViewInfo;
			application.Idling -= this.OnIdlingEvent;
			ShowGridApplication.m_gridNumberShowForm.Close();
			this.SaveShowState();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void OnRefreshGridsAndViewInfo(object sender, ViewActivatedEventArgs ViewActivated)
		{
			UIApplication uiapplication = sender as UIApplication;
			if (uiapplication == null)
			{
				return;
			}
			ShowGridApplication.m_gridNumberShowForm.UnLoad();
			this.m_uiDoc = uiapplication.ActiveUIDocument;
			if (!this.DrawGridNumber)
			{
				ShowGridApplication.m_gridNumberShowForm.Hide();
			}
			else if (!ShowGridApplication.m_gridNumberShowForm.Visible)
			{
				ShowGridApplication.m_gridNumberShowForm.Show(this.m_hWndRevit);
			}
			ShowGridApplication.m_gridNumberShowForm.RefreshDocAndView(this.m_uiDoc);
			uiapplication.ActiveUIDocument.Document.Regenerate();
		}

		private void OnIdlingEvent(object sender, IdlingEventArgs idlingArgs)
		{
			if (this.m_uiDoc == null)
			{
				return;
			}
			ShowGridApplication.m_gridNumberShowForm.DrawGridNumText(true);
		}

		public void ShowHideGridNumber()
		{
			if (this.DrawGridNumber)
			{
				YJKMessageBox.Show("全局轴号已关闭");
				ShowGridApplication.m_gridNumberShowForm.Hide();
			}
			else
			{
				ShowGridLevelNumOKForm showGridLevelNumOKForm = new ShowGridLevelNumOKForm();
				showGridLevelNumOKForm.ShowDialog();
				ShowGridApplication.m_gridNumberShowForm.SetShowLinkDocGridNum(showGridLevelNumOKForm.chkShowLink.Checked);
				ShowGridApplication.m_gridNumberShowForm.Show(this.m_hWndRevit);
				ShowGridApplication.m_gridNumberShowForm.UnLoad();
				ShowGridApplication.m_gridNumberShowForm.RefreshDocAndView(this.m_uiDoc);
			}
			this.DrawGridNumber = !this.DrawGridNumber;
		}

		private void SaveShowState()
		{
			string path = Product.UserDataLocation + "\\ShowGridNumber.txt";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			if (this.DrawGridNumber)
			{
				streamWriter.WriteLine("是");
			}
			else
			{
				streamWriter.WriteLine("否");
			}
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadShowState()
		{
			string path = Product.UserDataLocation + "\\ShowGridNumber.txt";
			if (!File.Exists(path))
			{
				this.DrawGridNumber = true;
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			if (streamReader.ReadLine() == "是")
			{
				this.DrawGridNumber = true;
			}
			else
			{
				this.DrawGridNumber = false;
			}
			streamReader.Close();
			fileStream.Close();
		}

		public static ShowGridApplication m_ThisApp;

		public static GridNumberForm m_gridNumberShowForm;

		public IntPtr m_hRevitHandle = IntPtr.Zero;

		private UIDocument m_uiDoc;

		private WindowHandle m_hWndRevit;
	}
}
